package main

import (
	"fmt"
	"strings"
	"unicode/utf8"
)

func main() {
	//strTest1()
	//strTest2()
	strTest3()
}

func strTest1() {
	str := `一二三四五
六七八\n\t\r`
	fmt.Println(str)

	s1 := "世界"
	s2 := "\xe4\xb8\x96\xe7\x95\x8c"
	s3 := "\u4e16\u754c"
	s4 := "\U00004e16\U0000754c"

	fmt.Println(s1)
	fmt.Println(s2)
	fmt.Println(s3)
	fmt.Println(s4)

	for _, s := range s1 {
		fmt.Println(s)
		fmt.Printf(`\u%x`, s)
		fmt.Println()
	}

	fmt.Println(strings.Contains(s1, s2))
	fmt.Println(contains(s1, s2))
	fmt.Println(hasPrefix(s1, s2))
	fmt.Println(hasSuffix(s1, s2))
}

func strTest2() {
	s := "hello, 世界"
	fmt.Println(len(s))
	fmt.Println(utf8.RuneCountInString(s))
	fmt.Println("---------------------")
	for i := 0; i < len(s); {
		r, size := utf8.DecodeRuneInString(s[i:])
		fmt.Printf("%d\t%c\n", i, r)
		i += size
	}
	fmt.Println("---------------------")

	for i, r := range s {
		fmt.Printf("%d\t%q\t%d\n", i, r, r)
	}
	fmt.Println("---------------------")
	n := 0
	for range s {
		n++
	}
	fmt.Println(n)
}

func strTest3() {
	s := "一二三四五六七"
	fmt.Printf("% x\n", s)
	f := []rune(s)
	fmt.Printf("%x\n", f)
	fmt.Println(string(f))

	fmt.Println(string(65))
	fmt.Println(string(0x4eac))

	fmt.Println(string(1234567))
	fmt.Printf("%b", ([]rune(string(1234567))[0]))
}

func hasSuffix(s, suffix string) bool {
	return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
}

func hasPrefix(s, preffix string) bool {
	return len(s) >= len(preffix) && s[:len(preffix)] == preffix
}

func contains(s, substr string) bool {
	for i := 0; i < len(s); i++ {
		if hasPrefix(s[i:], substr) {
			return true
		}
	}
	return false
}
